<?php

namespace app\common;
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/3/28
 * Time: 15:50
 */
use PHPExcel_IOFactory;

class php_excel
{
    public static function read_excel($excel,$date,$form,$require,$tid='')
    {
        $inputFileType = PHPExcel_IOFactory::identify($excel);

        $reader = PHPExcel_IOFactory::createReader($inputFileType); //设置以Excel5格式(Excel97-2003工作簿)

        $PHPExcel = $reader->load($excel); // 载入excel文件
        $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumm = $sheet->getHighestColumn(); // 取得总列数

        $data = [];
        $time = time();
        /** 循环读取每个单元格的数据 */
        for ($row = 2; $row <= $highestRow; $row++)    //行号从1开始
        {
            $temp = null;
            $temp['addtime'] = $temp['modtime'] = $time;
            $temp['date'] = $date;
            if($tid){
                $temp['tid'] = $tid;
            }
            foreach ($form as $key=>$value)
            {
                if($key =='J'|| $key== 'N'){
                    $cell = $sheet->getCell($key.$row)->getCalculatedValue();
                    $temp[$value] = is_object($cell)?$cell->__toString():$cell;
                }else{
                    $cell = $sheet->getCell($key.$row)->getFormattedValue();
                    $temp[$value] = is_object($cell)?$cell->__toString():$cell;
                }
            }
            if($temp[$require]){
                $data[] = $temp;
            }
        }
        return $data;
    }
}